package com.leaf.collegeidleapp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.leaf.collegeidleapp.adapter.ReviewAdapter;
import com.leaf.collegeidleapp.bean.Collection;
import com.leaf.collegeidleapp.bean.Review;
import com.leaf.collegeidleapp.toucheventtest.SideslipListView;
import com.leaf.collegeidleapp.util.MyCollectionDbHelper;
import com.leaf.collegeidleapp.util.ReviewDbHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class MyCollectionActivity_detail extends AppCompatActivity {

    TextView title, description, price, phone;
    ImageView ivCollectionDetail;
    TextView tvComment;
    ListView lvReview;
    LinkedList<Review> reviews = new LinkedList<>();
    EditText etComment;
    int position;
    byte[] picture;

    ReviewAdapter adapter;
    ReviewDbHelper dbHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection_detail);

        adapter = new ReviewAdapter(getApplicationContext());
        dbHelper = new ReviewDbHelper(getApplicationContext(), ReviewDbHelper.DB_NAME, null, 1);


        ivCollectionDetail = findViewById(R.id.iv_collectionDetail);
        title = findViewById(R.id.tv_title);
        description = findViewById(R.id.tv_description);
        price = findViewById(R.id.tv_price);
        phone = findViewById(R.id.tv_phone);
        final Bundle b = getIntent().getExtras();
        if (b != null) {
            picture = b.getByteArray("picture");
            Bitmap img = BitmapFactory.decodeByteArray(picture, 0, picture.length);
            ivCollectionDetail.setImageBitmap(img);
            title.setText(b.getString("title"));
            description.setText(b.getString("description"));
            price.setText(String.valueOf(b.getFloat("price")) + "元");
            phone.setText(b.getString("phone"));
            position = b.getInt("position");
        }

        //返回
        ImageButton ibBack = findViewById(R.id.ib_back);
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //进入聊天
        final String username = this.getIntent().getStringExtra("stuId");
        ImageButton ibTalk = findViewById(R.id.ib_talk);
        ibTalk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("stuId", username);
                Intent intent = new Intent(getApplicationContext(), ChatActivity.class);
                Toast.makeText(getApplicationContext(), username + "点击了", Toast.LENGTH_SHORT).show();
                intent.putExtras(bundle);
                //System.out.println(username);
                startActivity(intent);
            }
        });

        //进入聊天2
        ImageButton ibTalk2 = findViewById(R.id.ib_talk2);
        ibTalk2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("stuId", username);
                Intent intent = new Intent(getApplicationContext(), ReviewActivity.class);
                Toast.makeText(getApplicationContext(), username + "点击了", Toast.LENGTH_SHORT).show();
                intent.putExtras(bundle);
                //System.out.println(username);
                startActivity(intent);
            }
        });

        //点击收藏按钮
        ImageButton ibMyLove = findViewById(R.id.ib_my_love);
        ibMyLove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyCollectionDbHelper dbHelper = new MyCollectionDbHelper(getApplicationContext(), MyCollectionDbHelper.DB_NAME, null, 1);
                Collection collection = new Collection();
                collection.setTitle(title.getText().toString());
                String price1 = price.getText().toString().substring(0, price.getText().toString().length() - 1);
                collection.setPrice(Float.parseFloat(price1));
                collection.setPhone(phone.getText().toString());
                collection.setDescription(description.getText().toString());
                collection.setPicture(picture);
                String stuId = getIntent().getStringExtra("stuId");
                collection.setStuId(stuId);
                dbHelper.addMyCollection(collection);
                Toast.makeText(getApplicationContext(), "已添加至我的收藏!", Toast.LENGTH_SHORT).show();
            }
        });

        //设置item点击事件
        lvReview = findViewById(R.id.list_comment);
        lvReview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("stuId", username);
                Intent intent = new Intent(getApplicationContext(), ReviewActivity.class);
                Toast.makeText(getApplicationContext(), "点击了", Toast.LENGTH_SHORT).show();
                intent.putExtras(bundle);
                //System.out.println(username);
                startActivity(intent);
            }
        });




        /*
        etComment = findViewById(R.id.et_comment);
        //提交评论点击事件
        Button btnReview = findViewById(R.id.btn_submit);
        btnReview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //先检查是否为空
                if(CheckInput()) {
                    ReviewDbHelper dbHelper = new ReviewDbHelper(getApplicationContext(),ReviewDbHelper.DB_NAME,null,1);
                    Review review = new Review();
                    review.setContent(etComment.getText().toString());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
                    //获取当前时间
                    Date date = new Date(System.currentTimeMillis());
                    review.setCurrentTime(simpleDateFormat.format(date));
                    String stuId = getIntent().getStringExtra("stuId");
                    review.setStuId(stuId);
                    review.setPosition(position);
                    dbHelper.addReview(review);
                    //评论置为空
                    etComment.setText("");
                    Toast.makeText(getApplicationContext(),"评论成功!",Toast.LENGTH_SHORT).show();
                }
            }
        });
*/
        /*
        final ReviewAdapter adapter = new ReviewAdapter(getApplicationContext());
        final ReviewDbHelper dbHelper = new ReviewDbHelper(getApplicationContext(),ReviewDbHelper.DB_NAME,null,1);
        reviews = dbHelper.readReviews(position);
        adapter.setData(reviews);
        //设置适配器
        lvReview.setAdapter(adapter);


        //刷新页面
        TextView tvRefresh = findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reviews = dbHelper.readReviews(position);
                adapter.setData(reviews);
                lvReview.setAdapter(adapter);
            }
        });

         */
    }

    /**
     * 检查输入评论是否为空
     *
     * @return true
     */
    public boolean CheckInput() {
        String comment = etComment.getText().toString();
        if (comment.trim().equals("")) {
            Toast.makeText(this, "评论内容不能为空!", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }


    @Override
    protected void onResume() {
        super.onResume();
        reviews = dbHelper.readReviews(position);
        adapter.setData(reviews);
        lvReview.setAdapter(adapter);
    }
}